import os
from PyPDF2 import PdfReader
from docx import Document
import pandas as pd

def parse_file(file_path):
    """
    解析文件内容，支持 .txt, .docx, .xlsx 格式。
    返回解析后的文本内容。
    """
    if not os.path.exists(file_path):
        raise FileNotFoundError(f"File not found: {file_path}")
    
    _, ext = os.path.splitext(file_path)
    ext = ext.lower()

    if ext == '.txt':
        with open(file_path, 'r', encoding='utf-8') as f:
            return f.read()
    
    elif ext == '.docx':
        doc = Document(file_path)
        return "\n".join([para.text for para in doc.paragraphs])
    
    elif ext == '.xlsx':
        df = pd.read_excel(file_path)
        return df.to_string(index=False)
    
    else:
        raise ValueError(f"Unsupported file format: {ext}")
